﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SuperDeepZoom
{
    /// <summary>
    /// Assists with the arrangement of the msi overlay items on the screen.
    /// Aligns the overlay items to the sub images of the MSI
    /// </summary>
    internal class SpecialOverlayArranger
    {
        Dictionary<int, SpecialImageOverlayItem> items;

        MultiScaleImage msi;

        ImageArranger arranger;

        public SpecialOverlayArranger(ImageArranger arranger, MultiScaleImage msi)
        {
            this.arranger = arranger;
            this.msi = msi;
            items = new Dictionary<int, SpecialImageOverlayItem>();
        }

        internal void update()
        {
            foreach (var i in items)
            {
                int imageId = i.Key;

                if (msi.SubImages.Count > imageId)
                {
                    SpecialImageOverlayItem itm = i.Value;
                    if (arranger.ImagesToShow.Contains(msi.SubImages[imageId]))
                    {
                        itm.Visibility = Visibility.Visible;
                        Rect r = arranger.GetSubImageScreenRect(msi.SubImages[imageId]);

                        itm.Width = r.Width;
                        itm.Height = r.Height;

                        itm.SetValue(Canvas.TopProperty, r.Top);
                        itm.SetValue(Canvas.LeftProperty, r.Left);
                    }
                    else
                    {
                        itm.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    throw new Exception("Image ID not found in MSI");
                }
            }
        }

        public void Add(int id, SpecialImageOverlayItem item)
        {
            Remove(id);
            items.Add(id, item);
        }

        public void Remove(int id)
        {
            if (items.ContainsKey(id))
            {
                items.Remove(id);
            }
        }

    }
}
